<template>
  <app-list>
    <template #search>
      <div class="setting-title">分组管理</div>
    </template>
    <template #action>
      <el-button type="primary" class="hovepcvicecolor" icon="plus" @click="handleadd()">新增分组</el-button>
    </template>
    <el-table stripe :data="pageData.list" v-loading="pageData.loading">
      <el-table-column label="分组名称" prop="name" show-overflow-tooltip />
      <!-- <el-table-column label="分组类型" prop="type" show-overflow-tooltip>
        <template #default="scope">
          <div>{{ typeMap[scope.row.type] }}</div>
        </template>
      </el-table-column> -->
      <el-table-column label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="created_at" show-overflow-tooltip
        ><template #default="scope">
          <div>{{ scope.row.created_at }}</div>
        </template></el-table-column
      >
      <el-table-column label="开启/关闭">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            class="ml-2"
            @change="changeswitch(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80">
        <template #default="scope">
          <div style="display: flex">
            <div class="csspointer" @click.native.prevent="deleteRow(scope.row)">
              删除
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <template #footerRight>
      <el-pagination
        @size-change="pageActions.handleSizeChange"
        @current-change="pageActions.handlePageChange"
        v-model:current-page="pageData.page"
        v-model:page-size="pageData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
        background
      />
    </template>

    <AddItem v-model="dialog.edit.show" :data="dialog.edit.data" :parentdata="parentdata" @change="changeform()" />
  </app-list>
</template>
  
  <script setup>
import { http } from '@/utils/http'
import { ElMessage, ElMessageBox } from 'element-plus'

import AddItem from '../AddItem.vue'

const typeMap = ref({
  1: '代理-业务组',
  2: '代理-对盘组(对冲)',
  3: '代理-订单来源组',
  4: '代理-自定义组',
  5: '平台-业务组',
  6: '平台-对盘组(对冲)',
  7: '平台-订单来源组',
  8: '平台-自定义组',
  9: '第三方用户-自定义组',
})

const parentdata = ref({
  value: 'grouping',
  label: '新增分组'
})

const { pageData, pageActions, dialog } = usePage(
  () => {
    const query = {
      page: pageData.page,
      page_size: pageData.pageSize,
      ...pageData.query,
    }

    return http.get('/group/group_list', {
      params: query,
    })
  },
  {
    query: {},
  }
)

const timetrans = inject('timetrans')

// 新增
const handleadd = () => {
  parentdata.value.status = 'create'
  parentdata.value.label = '新增分组'
  pageActions.handleAdd()
}


const changeform = () => {
  pageActions.refresh(true)
}

// 改变开启状态
const changeswitch = row => {
  const onSuccess = () => {
    ElMessage.success('操作成功')
    changeform()
  }
  http({
    method: 'post',
    url: '/group/group_update',
    data: row,
  }).then(onSuccess)
}

// 删除
const deleteRow = row => {
  ElMessageBox.confirm('是否删除?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      http.delete(`/group/group_delete?id=${row.id}`).then(res => {
      ElMessage.success('删除成功')
      pageActions.refresh(true)
    })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消',
      })
    })
}
</script>
  
  <style lang="scss" scoped>
</style>